package vn.orderonline.core.dao.member;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.mysql.jdbc.StringUtils;

import vn.orderonline.core.commons.utils.DateUtils;
import vn.orderonline.core.dao.ResideDao;
import vn.orderonline.core.dao.common.GenericDaoImpl;
import vn.orderonline.core.dao.entity.Member;
import vn.orderonline.core.redis.RedisMember;

@Repository
public class MemberDaoImpl extends GenericDaoImpl<Member, Integer> implements
		MemberDao {
	
	@Autowired
	private ResideDao resideDao;

	@Override
	public RedisMember getRedisMemberByUsername(String username) {
		if (StringUtils.isNullOrEmpty(username)) {
			return null;
		}
		Criteria criteria = getSession().createCriteria(Member.class);
		criteria.add(Restrictions.eq("username", username));
		Member member = (Member) criteria.uniqueResult();
		if (member == null) {
			return null;
		}
		RedisMember redisMember = new RedisMember();
		redisMember.setMemberId(member.getMemberId());
		redisMember.setMemberName(member.getMemberName());
		redisMember.setMemberEmail(member.getMemberEmail());
		redisMember.setUsername(member.getUsername());
		redisMember.setMemberPassword(member.getMemberPassword());
		if (member.getBirthday() != null) {
			redisMember.setBirthday(DateUtils.dateToString(member.getBirthday(), "yyyy-MM-dd"));
		}
		redisMember.setGender(member.getGender() == true ? "Male" : "Female");
		redisMember.setWardName(member.getWardName());
		redisMember.setResideId(member.getResideId());
		redisMember.setResideName(resideDao.getResideNameByResideId(member.getResideId()));
		redisMember.setPhoneNumber(member.getPhoneNumber());
		redisMember.setCreateTime(DateUtils.timestampToString(member.getCreateTime(), "dd/MM/yyyy HH:mm:ss"));
		if (member.getUpdateTime() != null) {
			redisMember.setUpdateTime(DateUtils.timestampToString(member.getUpdateTime(), "dd/MM/yyyy HH:mm:ss"));
		}
		redisMember.setCreateId(member.getCreateId());
		redisMember.setUpdateId(member.getUpdateId());
		
		redisMember.setDescription(member.getDescription());
		return redisMember;
	}

	@Override
	public RedisMember getRedisMemberByEmail(String memberEmail) {
		if (StringUtils.isNullOrEmpty(memberEmail)) {
			return null;
		}
		Criteria criteria = getSession().createCriteria(Member.class);
		criteria.add(Restrictions.eq("memberEmail", memberEmail));
		Member member = (Member) criteria.uniqueResult();
		if (member == null) {
			return null;
		}
		RedisMember redisMember = new RedisMember();
		redisMember.setMemberId(member.getMemberId());
		redisMember.setMemberName(member.getMemberName());
		redisMember.setMemberEmail(member.getMemberEmail());
		redisMember.setUsername(member.getUsername());
		redisMember.setMemberPassword(member.getMemberPassword());
		redisMember.setBirthday(DateUtils.dateToString(member.getBirthday(), "yyyy-MM-dd"));
		redisMember.setGender(member.getGender() == true ? "Male" : "Female");
		redisMember.setWardName(member.getWardName());
		redisMember.setResideId(member.getResideId());
		redisMember.setResideName(resideDao.getResideNameByResideId(member.getResideId()));
		redisMember.setPhoneNumber(member.getPhoneNumber());
		redisMember.setCreateTime(DateUtils.timestampToString(member.getCreateTime(), "dd/MM/yyyy HH:mm:ss"));
		if (member.getUpdateTime() != null) {
			redisMember.setUpdateTime(DateUtils.timestampToString(member.getUpdateTime(), "dd/MM/yyyy HH:mm:ss"));
		}
		redisMember.setCreateId(member.getCreateId());
		redisMember.setUpdateId(member.getUpdateId());
		
		redisMember.setDescription(member.getDescription());
		return redisMember;
	}

	@Override
	public Boolean checkExistedEmail(String email) {
		Criteria criteria = getSession().createCriteria(Member.class);
		criteria.add(Restrictions.eq("memberEmail", email));
		return criteria.uniqueResult() != null ? true : false;
	}

	@Override
	public Boolean checkExistedUsername(String username) {
		Criteria criteria = getSession().createCriteria(Member.class);
		criteria.add(Restrictions.eq("username", username));
		return criteria.uniqueResult() != null ? true : false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RedisMember> search(String username, String email, String name,
			String phoneNumber, String wardName) {
		Criteria criteria = getSession().createCriteria(Member.class);
		if (!StringUtils.isNullOrEmpty(username)) {
			criteria.add(Restrictions.eq("username", username));
		}
		if (!StringUtils.isNullOrEmpty(email)) {
			criteria.add(Restrictions.eq("memberEmail", email));
		}
		if (!StringUtils.isNullOrEmpty(name)) {
			criteria.add(Restrictions.like("memberName", "%" + name + "%"));
		}
		if (!StringUtils.isNullOrEmpty(phoneNumber)) {
			criteria.add(Restrictions.like("phoneNumber", "%" + phoneNumber + "%"));
		}
		if (!StringUtils.isNullOrEmpty(wardName)) {
			criteria.add(Restrictions.like("wardName", "%" + wardName + "%"));
		}
		List<Member> list = criteria.list();
		List<RedisMember> listRe = null;
		if (!list.isEmpty()) {
			RedisMember redisMember = null;
			listRe = new ArrayList<>();
			for (Member me : list) {
				redisMember = new RedisMember();
				redisMember.setMemberId(me.getMemberId());
				redisMember.setMemberName(me.getMemberName());
				redisMember.setMemberEmail(me.getMemberEmail());
				redisMember.setUsername(me.getUsername());
				redisMember.setMemberPassword(me.getMemberPassword());
				redisMember.setPhoneNumber(me.getPhoneNumber());
				redisMember.setResideId(me.getResideId());
				redisMember.setWardName(me.getWardName());
				redisMember.setCreateTime(DateUtils.timestampToString(
						me.getCreateTime(), "dd/MM/yyyy HH:mm:ss"));
				listRe.add(redisMember);
				
			}
		}
		return listRe;
	}
	
}
